var constant = require('../const');
var models = require('../models');
var AV = require('leanengine')


function sendNoticeSms(req, res) {
	var query1 = new AV.Query(models.MLUser);
	var query2 = new AV.Query(models.MLUser);
	query1.containedIn("location_province", ["北京", "上海"]);
	query2.containedIn("location_city", ["广州", "深圳"]);

	var query = AV.Query.or(query1, query2);
	query.limit(1000);
	query.skip(1070);
	query.equalTo("isUserInited", 1);
	query.notEqualTo("lastLogin", null);
	query.notEqualTo("notifyPhoneNum", null);
	query.descending("lastLogin");

	query.find({
		success: function(results) {
			var num = results.length;
			var tmp = 0;
			setInterval(function() {
				var user = results[tmp];
				AV.Cloud.requestSmsCode({
					mobilePhoneNumber: user.get("notifyPhoneNum"),
					template: "notice",
					username: "用你单身的理由，换199元脱单俱乐部创始白金会员资格。微信公众号搜索“无独团”参与。2016年全新升级"
				}).then(function() {
					//发送成功
					console.log("成功" + tmp);
				}, function(err) {
					//发送失败
					res.error(err);
				});
				tmp++;
			}, 50);
		},
		error: function() {

		}
	})
}

exports.sendNoticeSms = sendNoticeSms;